﻿using Microsoft.AspNetCore.Mvc;
using mozhi.smarterp.Finance.Basedatas.Suppliers;
using mozhi.smarterp.Finance.Querys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;

namespace mozhi.smarterp.Finance.Basedatas
{
    [RemoteService]
    [Route("api/v1/suppliers")]
    public class SupplierController : BaseController
    {
        private ISupplierAppService _appService;
        public SupplierController(ISupplierAppService appService)
        {
            _appService = appService;
        }

        [HttpGet]
        [Route("all")]
        public async Task<List<SupplierDto>> GetAsync(string keyword)
        {
            var pageResult = await _appService.FindAsync(new PageQuery() { Keyword = keyword, PageIndex = 1, PageSize = int.MaxValue });

            var items = pageResult.Items;

            var result = items == null ? new List<SupplierDto>() : items.ToList();

            return result;
        }

        [HttpGet]
        public async Task<PagedResultDto<SupplierDto>> GetAsync(PageQuery query)
        {
            return await _appService.FindAsync(query);
        }

        [HttpGet]
        [Route("{id}")]
        public async Task<SupplierDto> GetAsync(Guid id)
        {
            var result = await _appService.GetAsync(id);

            return result;
        }

        [HttpPost]
        public async Task<SupplierDto> CreateAsync(SupplierCreateDto dto)
        {
            var result = await _appService.CreateAsync(dto);

            return result;
        }

        [HttpPut]
        public async Task<SupplierDto> UpdateAsync(SupplierUpdateDto dto)
        {
            var result = await _appService.UpdateAsync(dto);

            return result;
        }



        [HttpDelete]
        [Route("{id}")]
        public async Task DeleteAsync(Guid id)
        {
            await _appService.DeleteAsync(id);
        }
    }
}
